// pages/publish/publish.js
Page({

  /**
   * Page initial data
   */
  data: {
    title: '',
    categoryIndex: 0,
    categories: [
      { id: 1, name: '小麦秸秆' },
      { id: 2, name: '玉米秸秆' },
      { id: 3, name: '花生秧' },
      { id: 4, name: '大豆秸秆' },
      { id: 5, name: '饲料' }
    ],
    price: '',
    stock: '',
    region: [],
    description: '',
    images: [],
    isSubmitting: false
  },

  /**
   * Lifecycle function--Called when page load
   */
  onLoad(options) {

  },

  /**
   * Lifecycle function--Called when page is initially rendered
   */
  onReady() {

  },

  /**
   * Lifecycle function--Called when page show
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      })
    }
  },

  /**
   * Lifecycle function--Called when page hide
   */
  onHide() {

  },

  /**
   * Lifecycle function--Called when page unload
   */
  onUnload() {

  },

  /**
   * Page event handler function--Called when user drop down
   */
  onPullDownRefresh() {

  },

  /**
   * Called when page reach bottom
   */
  onReachBottom() {

  },

  /**
   * Called when user click on the top right corner to share
   */
  onShareAppMessage() {

  },

  // 选择商品类型
  onCategoryChange(e) {
    this.setData({
      categoryIndex: e.detail.value
    })
  },

  // 选择地区
  onRegionChange(e) {
    this.setData({
      region: e.detail.value
    })
  },

  // 选择图片
  chooseImage() {
    const that = this
    wx.chooseImage({
      count: 9 - that.data.images.length,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        that.setData({
          images: [...that.data.images, ...res.tempFilePaths]
        })
      }
    })
  },

  // 删除图片
  deleteImage(e) {
    const index = e.currentTarget.dataset.index
    const images = this.data.images
    images.splice(index, 1)
    this.setData({
      images
    })
  },

  // 提交表单
  handleSubmit() {
    if (this.data.isSubmitting) return
    
    // 表单验证
    if (!this.data.title.trim()) {
      return wx.showToast({
        title: '请输入商品标题',
        icon: 'none'
      })
    }
    
    if (!this.data.price) {
      return wx.showToast({
        title: '请输入商品价格',
        icon: 'none'
      })
    }
    
    if (!this.data.stock) {
      return wx.showToast({
        title: '请输入商品库存',
        icon: 'none'
      })
    }
    
    if (this.data.region.length === 0) {
      return wx.showToast({
        title: '请选择所在地区',
        icon: 'none'
      })
    }
    
    if (!this.data.description.trim()) {
      return wx.showToast({
        title: '请输入商品描述',
        icon: 'none'
      })
    }
    
    if (this.data.images.length === 0) {
      return wx.showToast({
        title: '请上传商品图片',
        icon: 'none'
      })
    }

    this.setData({
      isSubmitting: true
    })

    // 构建提交数据
    const formData = {
      title: this.data.title,
      category: this.data.categories[this.data.categoryIndex],
      price: Number(this.data.price),
      stock: Number(this.data.stock),
      region: this.data.region,
      description: this.data.description,
      images: this.data.images
    }

    // TODO: 调用后端API上传数据
    console.log('提交的表单数据：', formData)

    // 模拟提交
    setTimeout(() => {
      this.setData({
        isSubmitting: false
      })
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000,
        success: () => {
          setTimeout(() => {
            wx.switchTab({
              url: '/pages/index/index'
            })
          }, 2000)
        }
      })
    }, 1500)
  }
})